import { Node, UITransform } from "cc";
import { TYPE_LAYOUT_UI_AT } from "../xg-types";
import { display } from "../xg";

export class __XGameManager {

    /** 记录所有的UI层级 */
    private UILayouts: {[key: number]: Node} = {};

    private rootNode: Node;
    
    /** 一些初始化操作 */
    private init(root_node: Node){
        this.rootNode = root_node;
    }


    /** 添加UI层根节点 */
    public addUILayout(type: TYPE_LAYOUT_UI_AT, node_name: string) {
        if (this.UILayouts[type]) return;
        let _node = new Node(node_name);
        let tf = _node.addComponent(UITransform);
        tf.setContentSize(display.size);
        _node.setSiblingIndex(type);
        this.rootNode.addChild(_node);
        this.UILayouts[type] = _node;
    }

    /**
     * 获取指定UI层
     * @returns {Node}
     */
    public getUILayout(type: TYPE_LAYOUT_UI_AT): Node {
        return this.UILayouts[type];
    }


    public static init(root_node: Node){
        xGameMgr.init(root_node)
    };
    public static addUILayout(type: TYPE_LAYOUT_UI_AT, node_name: string){
        xGameMgr.addUILayout(type, node_name);
    }
}

/**
 * 全局管理器
 */
export const xGameMgr = new __XGameManager();